const jwt = require('jsonwebtoken')

const whiteList = ['/login', '/signIn']
const auth = async (ctx, next) => {
    //获取请求头里面的token值
    if (whiteList.includes(ctx.path)) {
        await next()
    }else{
        let { token } = ctx.request.header
        if (token) {
            try {
                const userInfo = jwt.verify(token, '1906A')
                await next()
            } catch (err) {
                ctx.body = {
                    code: 401,
                    message: 'token验证失败'
                }
            }

        } else {
            ctx.body = {
                code: 403,
                message: '没有token值'
            }
        }
    }
}
module.exports = auth